﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace SOA.DAL
{
    public class Filial
    {
        public SOA.MODEL.MetaFilial GetMetaByFilial(int idFilial)
        {            
            var dt = new Db().Query("SELECT * FROM TB_FILIAL WHERE ID= " + idFilial);
            if (dt.Rows.Count == 0)
                return new MODEL.MetaFilial();
            var v = new MODEL.MetaFilial();            
            v.MetaHotDog1 = Convert.ToDecimal(dt.Rows[0]["META_HOT_DOG_1"]);
            v.MetaHotDog2 = Convert.ToDecimal(dt.Rows[0]["META_HOT_DOG_2"]);
            v.MetaRefri1 = Convert.ToDecimal(dt.Rows[0]["META_REFRI_1"]);
            v.MetaRefri2 = Convert.ToDecimal(dt.Rows[0]["META_REFRI_2"]);
            v.IdFilial = idFilial;
            return v;
        }

        public List<SOA.MODEL.MetaFilial> GetMetas()
        {
            var dt = new Db().Query("SELECT * FROM TB_FILIAL WHERE IS_MATRIZ= 0");
            if (dt.Rows.Count == 0)
                return new List<MODEL.MetaFilial>();
            var lst = new List<MODEL.MetaFilial>();
            foreach (DataRow dr in dt.Rows)
            {
                var v = new MODEL.MetaFilial();
                v.MetaHotDog1 = Convert.ToDecimal(dr["META_HOT_DOG_1"]);
                v.MetaHotDog2 = Convert.ToDecimal(dr["META_HOT_DOG_2"]);
                v.MetaRefri1 = Convert.ToDecimal(dr["META_REFRI_1"]);
                v.MetaRefri2 = Convert.ToDecimal(dr["META_REFRI_2"]);
                v.IdFilial = Convert.ToInt32(dr["ID"]);
                lst.Add(v);
            }
            return lst;
        }

        public SOA.MODEL.Filial GetById(int idFilial)
        {
            var dt = new Db().Query("SELECT * FROM TB_FILIAL WHERE ID= " + idFilial);
            if (dt.Rows.Count == 0)
                return new MODEL.Filial();
            var v = new MODEL.Filial();
            v.Nome = dt.Rows[0]["NOME"].ToString();
            v.PontoNegativo = Convert.ToInt32(dt.Rows[0]["PONTO_NEGATIVO"]);            
            v.Id = idFilial;
            return v;
        }

        public List<SOA.MODEL.Filial> GetFiliais()
        {
            var dt = new Db().Query("SELECT * FROM TB_FILIAL WHERE IS_MATRIZ= 0");
            if (dt.Rows.Count == 0)
                return new List<MODEL.Filial>();
            var lst = new List<MODEL.Filial>();
            foreach (DataRow dr in dt.Rows)
            {
                var v = new MODEL.Filial();
                v.Nome = dr["NOME"].ToString();
                v.PontoNegativo = Convert.ToInt32(dr["PONTO_NEGATIVO"]);
                v.Id = Convert.ToInt32(dr["ID"]);
                lst.Add(v);
            }
            return lst;            
        }

        public int GetPontuacaoByFilial(int idFilial)
        {
            var v = GetById(idFilial);
            if (v == null)
                return 0;
            return v.PontoNegativo;
        }

        public bool AdicionarPontoNegativo(int idFilial)
        {
            var pontos = GetPontuacaoByFilial(idFilial) + 1;
            var sqlUpdate = "UPDATE TB_FILIAL SET PONTO_NEGATIVO = " + pontos + " ";            
            sqlUpdate += "WHERE ID = " + idFilial;

            return new Db().Execute(sqlUpdate) > 0;
        }

    }
}